package de.dfki.asr.genesis2.sms.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.http.HttpStatus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import de.dfki.asr.genesis2.sms.servlets.LocationSearch.GetThread;
import de.dfki.asr.genesis2.sms.util.FacebookPOIXMLExtractor;
import de.dfki.asr.genesis2.sms.util.FlickrPOIXMLExtractor;
import de.dfki.asr.genesis2.sms.util.SocialPOIXMLExtractor;
import de.dfki.asr.genesis2.sms.util.TwitterPOIXMLExtractor;
import de.dfki.asr.genesis2.sms.util.YoutubePOIXMLExtractor;

/**
 * Servlet implementation class UserSpecifc
 */
@WebServlet("/userfeeds")
public class UserSpecifc extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static final String FACEBOOK_HOME_WITH_LOCATION = "https://graph.facebook.com/me/home?with=location&limit=9999999&since=2010-01-01&date_format=U";
	private static final int defaultMaxResults = 50;
	
	private static final SocialPOIXMLExtractor youtubeExtractor = new YoutubePOIXMLExtractor();
	private static final SocialPOIXMLExtractor twittwerExtractor = new TwitterPOIXMLExtractor();
	private static final SocialPOIXMLExtractor facebookExtractor = new FacebookPOIXMLExtractor();
	private static final SocialPOIXMLExtractor flickrExtractor = new FlickrPOIXMLExtractor();

	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserSpecifc() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String sources = request.getParameter("sources");
		String since = request.getParameter("since");
		String maxResults = request.getParameter("max-results");
		
		sources = "facebook";
		
		// Check Max-Results parameter
		if (maxResults==null || maxResults.equals("")) {
			maxResults = Integer.toString(defaultMaxResults);
		}
		else {
			if (!maxResults.matches("[0-9]+") || Integer.parseInt(maxResults) <= 0 || Integer.parseInt(maxResults) > 50) {
				response.sendError(HttpStatus.SC_BAD_REQUEST, "Invalid max-results parameter, it should be a positive integer not greater than 50 ");
				return;
			}
		}
		
		// Check Sources parameter
		boolean twitter = false, youtube = false, flickr = false, facebook = false;
		if (sources==null || sources=="") {
			twitter = true;
			youtube = true;
			flickr =true;
			facebook = true;
		}
		else {
			if (sources.contains("twitter")) twitter = true;
			if (sources.contains("youtube")) youtube = true;
			if (sources.contains("flickr"))  flickr  = true;
			if (sources.contains("facebook"))  facebook  = true;
			if (youtube==false && twitter==false && flickr==false && facebook==false) {
				response.sendError(HttpStatus.SC_BAD_REQUEST, "Invalid sources parameter, it should be a CSV subset of {twitter,youtube,facebook,flickr}. Example: sources=twitter ");
				return;
			}
		}
		
        HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
		GetMethod get = null;
		PrintWriter out = response.getWriter();

		out.write("<pois>\n");

		ArrayList<Thread> thread = new ArrayList<Thread>();
		//try {	
			if (facebook) {
				String facebookURL = FACEBOOK_HOME_WITH_LOCATION + "&access_token=AAAFsBCj8RD4BAMJvuzWhD2DkcXoFzDvauds0xoZBRMX8373abuMrGGwyLwajqXAUw8Hhft8gyOXLePCj425fGA8tkayVziXLybyNiRQZDZD";
				get = new GetMethod(facebookURL);
				client.executeMethod(get);  
				String body = get.getResponseBodyAsString(); 
				System.out.println(body);
				String pois = facebookExtractor.extractPOIXMLsFromResponse(body);
				
				out.write(pois);
			}
		//}
		
		out.write("</pois>");
			
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
